package com.timepies.persistence;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

//http://www.html.it/pag/49180/database-e-sqlite/
public class DbManager {
	private DBhelper dbhelper;

	public DbManager(Context ctx) {
		dbhelper = new DBhelper(ctx);
	}

	public void save(String sub, String txt, String date) {
		SQLiteDatabase db = dbhelper.getWritableDatabase();

		ContentValues cv = new ContentValues();
		cv.put(DatabaseStrings.FIELD_SUBJECT, sub);
		cv.put(DatabaseStrings.FIELD_TEXT, txt);
		cv.put(DatabaseStrings.FIELD_DATE, date);
		try {
			db.insert(DatabaseStrings.TBL_NAME, null, cv);
		} catch (SQLiteException sqle) {
			// Gestione delle eccezioni
		}
	}

	public void update(String sub, String txt, String date, long id) {
		SQLiteDatabase db = dbhelper.getWritableDatabase();

		ContentValues cv = new ContentValues();
		cv.put(DatabaseStrings.FIELD_SUBJECT, sub);
		cv.put(DatabaseStrings.FIELD_TEXT, txt);
		cv.put(DatabaseStrings.FIELD_DATE, date);
		try {
			db.update(DatabaseStrings.TBL_NAME, cv, DatabaseStrings.FIELD_ID
					+ "=?", new String[] { Long.toString(id) });
		} catch (SQLiteException sqle) {
			// Gestione delle eccezioni
		}
	}

	public boolean delete(long id) {
		SQLiteDatabase db = dbhelper.getWritableDatabase();
		try {
			if (db.delete(DatabaseStrings.TBL_NAME, DatabaseStrings.FIELD_ID
					+ "=?", new String[] { Long.toString(id) }) > 0)
				return true;
			return false;
		} catch (SQLiteException sqle) {
			return false;
		}

	}

	public Cursor query() {
		Cursor crs = null;
		try {
			SQLiteDatabase db = dbhelper.getReadableDatabase();
			crs = db.query(DatabaseStrings.TBL_NAME, null, null, null, null,
					null, null, null);
		} catch (SQLiteException sqle) {
			return null;
		}
		return crs;
	}

}